<template>
  <div>
    <el-table :data="tableData" border stripe :header-cell-style="{background: '#cccccc'}"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50">
      </el-table-column>
      <el-table-column prop="id" label="ID" width="50">
      </el-table-column>
      <el-table-column prop="patientName" label="患者姓名" width="100">
      </el-table-column>
      <el-table-column prop="doctorName" label="医生姓名" width="100">
      </el-table-column>
      <el-table-column prop="content" label="病历详情" width="150">
        <template slot-scope="scope">
          <el-button type="primary" @click="view(scope.row)">查看内容</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注">
      </el-table-column>
      <el-table-column prop="createTime" label="创建日期">
      </el-table-column>
    </el-table>
    <!--分页-->
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <!--新增弹出框-->
    <el-dialog title="病历信息" :visible.sync="dialogFormVisible" width="60%" ref="img">
      <el-form :model="form" size="small">
        <el-form-item label="患者用户名" label-width="100px">
          <el-input v-model="form.patientName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注" label-width="100px">
          <el-input v-model="form.remark" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="病历内容" label-width="100px">
          <div id="richText"></div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
    <!--详细内容弹出框-->
    <el-dialog title="病历详情" :visible.sync="detailDialogFormVisible" width="60%" ref="img">
      <el-descriptions size="medium">
        <el-descriptions-item label="患者姓名"><el-link :underline="false" style="font-weight: bold">{{ medicalRecordDetail.patientName }}</el-link></el-descriptions-item>
        <el-descriptions-item label="医生姓名"><el-link :underline="false" style="font-weight: bold">{{ medicalRecordDetail.doctorName }}</el-link></el-descriptions-item>
        <el-descriptions-item label="备注">
          <el-tag size="small" type="danger">{{ medicalRecordDetail.remark }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="日期"><el-link :underline="false" type="warning">{{ medicalRecordDetail.createTime }}</el-link></el-descriptions-item>
      </el-descriptions>
      <el-divider></el-divider>
      <el-button type="text" style="font-size: 24px;margin-bottom: 15px">诊断结果</el-button>
      <div v-html="medicalRecordDetail.content"></div>
    </el-dialog>
  </div>
</template>

<script>
import E from "wangeditor"
import {serverIp, serverPort} from "../../../../public/config";
let editor;

export default {
  name: "MedicalRecord",
  data() {
    return {
      tableData: [],
      title:"",
      form: {},
      dialogFormVisible: false,
      detailDialogFormVisible: false,
      multipleSelection: [],
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      //分页
      pageNum: 1,
      pageSize: 5,
      total: 0,
      patientName: "",
      doctorName: "",
      medicalRecordDetail: {}
    }
  },
  methods: {
    load() {
      this.request.get("/medicalRecord/personMedicalRecord",{
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize
        }
      }).then(res => {
        console.log(res);
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    view(medicalRecord) {
      this.medicalRecordDetail.patientName = medicalRecord.patientName
      this.medicalRecordDetail.doctorName = medicalRecord.doctorName
      this.medicalRecordDetail.content = medicalRecord.content
      this.medicalRecordDetail.remark = medicalRecord.remark
      this.medicalRecordDetail.createTime = medicalRecord.createTime
      this.detailDialogFormVisible = true
    },
    handleImgUploadSuccess(res) {
      this.$message.success("上传成功")
      this.form.img = res
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {img: ''}
      this.$nextTick(() => {
        if (!editor) {
          editor = new E("#richText")
          editor.config.uploadImgServer = `http://${serverIp}:${serverPort}/file/uploadImg`
          editor.config.uploadFileName = 'file'
          editor.create()
        }
        //清除内容
        editor.txt.html('')
        if (this.$refs.img) {
          this.$refs.img.clearFiles()
        }
        if (this.$refs.file) {
          this.$refs.file.clearFiles()
        }
      })
    },
    save() {
      const content = editor.txt.html()
      //手动赋值
      this.form.content = content
      this.request.post("/medicalRecord/save",this.form).then(res => {
        console.log(this.form)
        console.log(res)
        if (res.code === '200') {
          this.dialogFormVisible = false
          this.form = {}
          this.$message.success("保存成功")
          this.load()
        }else {
          this.$message.error("保存失败")
        }
      })
    },
    handleEdit(row) {
      console.log("edit")
      console.log(row)
      this.form = Object.assign({},row)
      this.dialogFormVisible = true
      this.$nextTick(() => {
        if (!editor) {
          editor = new E("#richText")
          editor.config.uploadImgServer = `http://${serverIp}:${serverPort}/file/uploadImg`
          editor.config.uploadFileName = 'file'
          editor.create()
        }
        editor.txt.html(this.form.content)
        if (this.$refs.img) {
          this.$refs.img.clearFiles()
        }
        if (this.$refs.file) {
          this.$refs.file.clearFiles()
        }
      })
    },
    del(id) {
      this.request.delete("/medicalRecord/"+id).then(res => {
        if (res.code === '200') {
          this.$message.success("删除成功")
          this.load()
        }else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    delBatch() {
      let ids = this.multipleSelection.map(v => v.id)
      this.request.post("/medicalRecord/del/batch",ids).then(res => {
        if (res.data) {
          this.$message.success("批量删除成功")
          this.load()
        }else {
          this.$message.error("批量删除失败")
        }
      })
    },
    reset() {
      this.patientName = ""
      this.doctorName = ""
      this.load()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.load()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val
      this.load()
    }
  },
  created() {
    this.load()
  },
  watch: {
    dialogFormVisible() {
      if (this.dialogFormVisible === false) {
        this.form = {}
        this.form.department = {}
        console.log("执行了watch方法")
      }
    }
  }

}
</script>

<style>
.el-header {
  color: #333;
  line-height: 60px;
}

.el-aside {
  color: #333;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>